#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/11/30 9:56
# @Author  : FuKai ZHANG
# @File    : login_page.py
# @Description :login_page  pageobject
from Pages.Basepage import BasePage
from utils.PageConfigFiles_tools.get_PageElementconFile import ParseConFile
from utils import config
from utils.Logging_tools.log_control import INFO


class LoginPage(BasePage):
    username_input = ParseConFile().get_locators("LoginPage", "username_input")
    password_input = ParseConFile().get_locators("LoginPage", "password_input")
    login_button = ParseConFile().get_locators("LoginPage", "login_button")
    login_success_assert = ParseConFile().get_locators("LoginPage", "login_success_assert")

    def input_username(self, username):
        """输入用户名"""
        # 等待输入框可见，然后send_keys
        self.wait_element_to_be_located(*LoginPage.username_input, model="用户名输入框")
        self.send_keys(*LoginPage.username_input, model="用户名输入框", value=username)

    def input_password(self, password):
        """输入密码"""
        # 等待输入框可见，然后send_keys
        self.wait_element_to_be_located(*LoginPage.password_input, model="密码输入框")
        self.send_keys(*LoginPage.password_input, model="密码输入框", value=password)

    def click_login_button(self):
        """点击登录按钮"""
        # 等待登录按钮可点击，然后点击
        self.click(*LoginPage.login_button, model="登录按钮")

    def login_error_alert(self):
        """登录错误弹窗"""
        self.switch_to_alert()
        alert_text = self.get_alert_text(model="登录错误弹窗")
        self.alert_accept()
        return alert_text

    def login_success(self):
        self.wait_element_to_be_located(*LoginPage.login_success_assert, model="登录成功断言")
        return self.get_element_text(*LoginPage.login_success_assert, model="登录成功断言")

    def login(self, username, password):
        """登录流程"""
        INFO.logger.info("<=执行登录操作=>")
        self.load_url(config.project_url)
        self.input_username(username)
        self.input_password(password)
        self.click_login_button()
